using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerInput : MonoBehaviour
{
    private PlayerStateMachine stateMachine;

    [SerializeField] private KeyCode JumpKey;
    [SerializeField] private KeyCode ThrowKey;
    [SerializeField] private KeyCode RecycleKey;
    

    private void Awake()
    {
        stateMachine = GetComponent<PlayerStateMachine>();
    }

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            UIManager.Instance.SetPausePanel();
        }
        
        if (stateMachine.canInput)
        {
            var curDirectionControl = new Vector3()
            {
                x = Input.GetAxisRaw("Horizontal")
            };
            stateMachine.playerInputDirction = curDirectionControl;
            
            if (Input.GetAxis("Horizontal") != 0)
                    {
                        stateMachine.HandleInput(EPlayerInput.Move,EInputType.Holding);
                    }else if (Input.GetAxis("Horizontal") == 0)
                    {
                        stateMachine.HandleInput(EPlayerInput.Move,EInputType.End);
                    }
            
                    if (Input.GetKeyDown(JumpKey))
                    {
                        stateMachine.HandleInput(EPlayerInput.Jump,EInputType.Begin);
                    }
                    else if (Input.GetKey(JumpKey))
                    {
                        stateMachine.HandleInput(EPlayerInput.Jump,EInputType.Holding);
                    }
                    else if(Input.GetKeyUp(JumpKey))
                    {
                        stateMachine.HandleInput(EPlayerInput.Jump,EInputType.End);
                    }
            
                    if (Input.GetKeyDown(ThrowKey))
                    {
                        stateMachine.HandleInput(EPlayerInput.Throw,EInputType.Begin);
                    }
                    else if (Input.GetKey(ThrowKey))
                    {
                        stateMachine.HandleInput(EPlayerInput.Throw,EInputType.Holding);
                    }
                    else if (Input.GetKeyUp(ThrowKey))
                    {
                        stateMachine.HandleInput(EPlayerInput.Throw,EInputType.End);
                    }
            
                    if (Input.GetKeyDown(RecycleKey) && !PlayerAttribute.Instance.hasArrow)
                    {
                        PlayerAttribute.Instance.ArrowRecycle();
                    }
        }
    }
}

public enum EPlayerInput
{
    Move,Jump,Throw
}

public enum EInputType
{
    Begin,Holding,End
}